﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Windows.Input;

namespace C7
{
    public class WindowOperations
    {
        private Rectangle rect;
        private Storyboard sb;
        private static Panel root;
        private Dictionary<TextBox, string> dTextBoxText = new Dictionary<TextBox, string>();
        private enum TextBoxOperation
        {
            Fill,
            Empty,
            Store,            
        }

        private void CloseWindow()
        {
            if (rect != null)
            {
                if (rect.Parent is Panel)
                {
                    var panel = rect.Parent as Panel;
                    panel.Children.Remove(rect);
                } 
            }
            if (root.Parent is Panel)
            {
                var panel = root.Parent as Panel;
                panel.Children.Remove(root.Parent as FrameworkElement);
            }
            Info.module = null;
        }

        private void OpenWindowAnimation(FrameworkElement obj)
        {
            var taOpen = new ThicknessAnimation();
            var sb = new Storyboard();
            var panel = obj.Parent as Panel;
            taOpen.From = new Thickness(panel.ActualWidth, 0, panel.ActualWidth, 0);
            taOpen.To = new Thickness(0);
            taOpen.Duration = new Duration(TimeSpan.FromSeconds(1));
            Storyboard.SetTargetName(taOpen, "LayoutRoot");
            Storyboard.SetTargetProperty(taOpen, new PropertyPath(Grid.MarginProperty));
            sb.Children.Add(taOpen);
            sb.Begin(obj);
        }

        public void CloseWindowAnimation(FrameworkElement obj)
        {
            var taClose = new ThicknessAnimation();
            sb = new Storyboard();
            sb.Completed += sb_Completed;
            taClose.To = new Thickness(obj.ActualWidth, 0, obj.ActualWidth, 0); ;
            taClose.Duration = new Duration(TimeSpan.FromSeconds(1));
            Storyboard.SetTargetName(taClose, "LayoutRoot");
            Storyboard.SetTargetProperty(taClose, new PropertyPath(Grid.MarginProperty));
            sb.Children.Add(taClose);
            sb.Begin(obj);
        }

        private void sb_Completed(object sender, EventArgs e)
        {
            CloseWindow();//GetFirstParent(sender as FrameworkElement) as FrameworkElement);
        }

        public void FadeBackgroundAndDisable(Panel p)
        {
            rect = new Rectangle();
            var clr = new Color();
            clr = (Color)ColorConverter.ConvertFromString("#99FFFFFF");
            rect.Fill = new SolidColorBrush(clr);
            p.Children.Add(rect);
            Grid.SetColumnSpan(rect, 99);
            Grid.SetRowSpan(rect, 99);
        }

        public void PauseOrResumeAllAnimations(FrameworkElement fe, bool IsPausing)
        {
            foreach (DictionaryEntry i in fe.Resources)
            {

                if (fe.TryFindResource(i.Key) is Storyboard)
                {
                    var sb = (Storyboard)fe.TryFindResource(i.Key);
                    if (IsPausing)
                    {
                        sb.Pause(fe);
                    }
                    else
                    {
                        sb.Resume(fe);
                    } 
                }
            }

            if (fe.Parent is FrameworkElement)
            {
                PauseOrResumeAllAnimations(fe.Parent as FrameworkElement, IsPausing);
            }
        }

        private void EmptyOrFillTextBox(TextBox tb, TextBoxOperation tbo)
        {
            if (tbo == TextBoxOperation.Empty)
            {
                dTextBoxText[tb] = tb.Text;
                tb.Text = string.Empty;
                tb.FontFamily = new FontFamily("Zegoe UI - U");
            }
            else if (tbo == TextBoxOperation.Fill)
            {
                tb.Text = dTextBoxText[tb];
            }
            else
            {
                dTextBoxText[tb] = tb.Text;
            }
        }

        public void TextBoxesToHandlers(Panel p)
        {
            if (p != null)
            {
                foreach (FrameworkElement i in p.Children)
                {
                    if (i is TextBox)
                    {
                        (i as TextBox).GotFocus += TextBoxGotFocus;
                        (i as TextBox).LostFocus += TextBoxLostFocus;
                        (i as TextBox).KeyUp += TextBoxKeyUp;
                    }
                    else if (i is Panel)
                    {
                        TextBoxesToHandlers(i as Panel);
                    }
                    else if (i is Border)
                    {
                        TextBoxesToHandlers((i as Border).Child as Panel);
                    }
                    else if (i is ScrollViewer)
                    {
                        TextBoxesToHandlers(VisualTreeHelper.GetChild(i as ScrollViewer, 1) as Panel);
                    }
                    else if (i is Viewbox)
                    {
                        TextBoxesToHandlers((i as Viewbox).Child as Panel);
                    }
                } 
            }
        }

        private void TextBoxGotFocus(object sender, RoutedEventArgs e)
        {
            EmptyOrFillTextBox(sender as TextBox, WindowOperations.TextBoxOperation.Empty);
        }

        private void TextBoxLostFocus(object sender, RoutedEventArgs e)
        {
            EmptyOrFillTextBox(sender as TextBox, WindowOperations.TextBoxOperation.Fill);
        }

        private void TextBoxKeyUp(object sender, KeyEventArgs e)
        {
            (sender as TextBox).GotFocus -= TextBoxGotFocus;
            (sender as TextBox).LostFocus -= TextBoxLostFocus;

        }

        public void WindowToAnimationHandlers(Panel p, bool FirstParent)
        {
            if (p.Parent != null && FirstParent)
            {
                (p.Parent as FrameworkElement).Loaded += BaseWindowLoaded;
            }
            foreach (UIElement i in p.Children)
            {
                if (i is Panel)
                {
                    WindowToAnimationHandlers(i as Panel, false);
                }

                if (i is StackPanel)
                {
                    if ((i as StackPanel).Name == "spClose")
                    {
                        (i as StackPanel).MouseLeftButtonUp += spClose_MouseLeftButtonUp;
                    }
                }
            }
        }

        private void spClose_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            CloseWindowAnimation(GetFirstParent(sender as FrameworkElement));
            PauseOrResumeAllAnimations((sender as FrameworkElement).Parent as FrameworkElement, false);
        }

        private void BaseWindowLoaded(object sender, System.Windows.RoutedEventArgs e)
        {
            OpenWindowAnimation(sender as FrameworkElement);
        }

        private FrameworkElement GetFirstParent(FrameworkElement fe)
        {
            if (fe.Parent != null)
            {
                if (fe.Parent is Panel)
                {
                    if ((fe.Parent as Panel).Name == "LayoutRoot")
                    {
                        root = fe.Parent as Panel;
                        return (fe.Parent as FrameworkElement);
                    }
                }
               return GetFirstParent(fe.Parent as FrameworkElement);
            }
            return fe;
        }
    }
}
